package com.xxr.mom.ci.charging.interworking.v2016.accepter.query;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxr.mom.ci.charging.device.entity.Gun;
import com.xxr.mom.ci.charging.device.entity.Pile;
import com.xxr.mom.ci.charging.device.entity.Station;
import com.xxr.mom.ci.charging.interworking.base.InterfaceAccepter;
import com.xxr.mom.ci.charging.interworking.dto.InterworkingConfigDto;
import com.xxr.mom.ci.charging.interworking.v2016.AbstractV2016Service;
import com.xxr.mom.ci.charging.interworking.vo.ConnectorInfo;
import com.xxr.mom.ci.charging.interworking.vo.EquipmentInfo;
import com.xxr.mom.ci.charging.interworking.vo.StationInfo;
import com.xxr.mom.ci.charging.interworking.vo.api.QueryStationsInfoRequest;
import com.xxr.mom.ci.charging.interworking.vo.api.QueryStationsInfoResponse;
import com.xxr.mom.ci.charging.mapper.device.GunMapper;
import com.xxr.mom.ci.charging.mapper.device.PileMapper;
import com.xxr.mom.ci.charging.mapper.device.StationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询充电站信息
 * 概述：此接口用于查询运营商的充电站的信息
 * 接口名称：query_stations_info
 * 接口使用方法:由基础设施运营商方实现此接口，数据需求方调用
 * 查询时需要比对电站、充电设备、充电接口的基本信息的最后修改时间，三者只要有一处修改，就认为是最新修改时间，然后与输入参数lastQueryTime进行对比
 *
 * @author natsuki_kining
 */
@Service
public class QueryStationsInfoAccepterService extends AbstractV2016Service<QueryStationsInfoRequest, QueryStationsInfoResponse> implements InterfaceAccepter<QueryStationsInfoRequest, QueryStationsInfoResponse> {

    @Autowired
    private GunMapper gunMapper;
    @Autowired
    private PileMapper pileMapper;
    @Autowired
    private StationMapper stationMapper;

    @Override
    public QueryStationsInfoResponse dataHandler(QueryStationsInfoRequest queryStationsInfoRequest, InterworkingConfigDto configDto) {
        Page<Station> page = new Page<>(queryStationsInfoRequest.getPageNo(), queryStationsInfoRequest.getPageSize());
        Page<Station> pageStation = stationMapper.page(page, queryStationsInfoRequest);
        QueryStationsInfoResponse queryStationsInfoResponse = new QueryStationsInfoResponse();
        queryStationsInfoResponse.setPageNo(queryStationsInfoRequest.getPageNo());
        queryStationsInfoResponse.setPageCount(pageStation.getPages());
        queryStationsInfoResponse.setItemSize(pageStation.getTotal());
        queryStationsInfoResponse.setStationInfos(this.getStationInfos(pageStation.getRecords()));
        return queryStationsInfoResponse;
    }

    private List<StationInfo> getStationInfos(List<Station> stations) {
        List<StationInfo> stationInfos = new ArrayList<>();
        if (CollectionUtil.isEmpty(stations)) {
            return stationInfos;
        }
        stations.forEach(station -> {
            StationInfo stationInfo = new StationInfo();
            stationInfo.setStationId(station.getStationNumber());
            stationInfo.setOperatorId("");
            stationInfo.setEquipmentOwnerId("");
            stationInfo.setStationName(stationInfo.getStationName());
            stationInfo.setCountryCode("");
            stationInfo.setAreaCode("");
            stationInfo.setAddress("");
            stationInfo.setStationTel("");
            stationInfo.setStationType(1);
            stationInfo.setStationStatus(50);
            stationInfo.setParkNums(0);
            stationInfo.setStationLng(0D);
            stationInfo.setStationLat(0D);
            stationInfo.setSiteGuide("");
            stationInfo.setConstruction(255);
            stationInfo.setPictures(new ArrayList<>());
            stationInfo.setMatchCars("");
            stationInfo.setParkInfo("");
            stationInfo.setBusineHours("");
            stationInfo.setElectricityFee("");
            stationInfo.setServiceFee("");
            stationInfo.setParkFee("");
            stationInfo.setPayment("");
            stationInfo.setSupportOrder(0);
            stationInfo.setRemark("");
            stationInfo.setEquipmentInfos(this.getEquipmentInfos(station));
            stationInfos.add(stationInfo);
        });

        return stationInfos;
    }

    private List<EquipmentInfo> getEquipmentInfos(Station station) {
        List<EquipmentInfo> equipmentInfos = new ArrayList<>();
        LambdaQueryWrapper<Pile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Pile::getStationId, station.getId());
        List<Pile> piles = pileMapper.selectList(queryWrapper);
        if (CollectionUtil.isEmpty(piles)) {
            return equipmentInfos;
        }
        piles.forEach(pile -> {
            EquipmentInfo equipmentInfo = new EquipmentInfo();
            equipmentInfo.setConnectorInfos(this.getConnectorInfos(pile));
            equipmentInfos.add(equipmentInfo);
        });
        return equipmentInfos;
    }

    private List<ConnectorInfo> getConnectorInfos(Pile pile) {
        List<ConnectorInfo> connectorInfos = new ArrayList<>();
        LambdaQueryWrapper<Gun> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Gun::getPileId, pile.getId());
        List<Gun> guns = gunMapper.selectList(queryWrapper);
        if (CollectionUtil.isEmpty(guns)) {
            return connectorInfos;
        }
        guns.forEach(gun -> {
            ConnectorInfo connectorInfo = new ConnectorInfo();
            connectorInfo.setConnectorId(gun.getId() + "");
            connectorInfos.add(connectorInfo);
        });
        return connectorInfos;
    }

}
